
define(["js/mBox.js"],function(Box){  
	
	
	/*道具列表
	 * item/list   所有道具
	item/list/{userId} 用户所有道具
	item/list/{userId}/{roomId}
	 */
	
	var props = {
			/*所有道具卡*/
			allList:function(){
				var _t = this;
				var url = Game.getBaseURL() + "/item/list";
				$.ajax({
					url: url,
					dataType:"json",
					success: function(data){
						if(data.success){
							_t.createAllList(data.data.items);
						}
					}
				});
			},
			
			/*自己有的道具卡*/
			myList:function(){
				var _t = this;
				var id = dataStorage.get(MyOwner.userId);
				var url = Game.getBaseURL() + "/item/list/"+id;
				$.ajax({
					url: url,
					dataType:"json",
					success: function(data){
						if(data.success){
							_t.createMyList(data.data.userItems);
						}
					}
				});
			},
			
			/*在房间可以使用道具卡*/
			listRoom:function(){
				var _t = this;
				var id = dataStorage.get(MyOwner.userId);
				var roomId = dataStorage.get(MyOwner.roomId);
				var url = Game.getBaseURL() + "/item/list/"+id+"/"+roomId;
				$.ajax({
					url: url,
					dataType:"json",
					success: function(data){
						if(data.success){
							_t.createItemList(data.data.userItems);
						}
					}
				});
			},
			
			createMyList:function(data){
				var propsArr = [];
				for(var i=0; i<data.length; i++){
					propsArr.push("<li>"+
								    "<span style='width:80px'><span class='props_icon props_"+data[i].token+"'></span></span>"+
								    "<span style='width:70px'>x"+data[i].num+"</span>"+
								    "<span style='width:80px'>"+data[i].itemName+"</span>"+
								  "</li>");
				}
				console.log(propsArr.join(""))
				$("#propsLists").html(propsArr.join(""));
			},
			
			createAllList:function(data){
				var _t = this;
				var lists = [];
				for(var i=0; i<data.length; i++){
					if(i%2 ===0){
						var background = "background:#e6c3bd";
					}else{
						var background = "";
					}
					lists.push("<tr style='"+background+"'>"+
							    "<td style='width:15%'><span class='props_icon props_"+data[i].token+"'></span></td>"+
							    "<td style='width:20%'>"+data[i].name+"</td>"+
							    "<td style='width:35%'>"+data[i].description+"</td>"+
							    "<td style='width:15%'>"+data[i].price+"</td>"+
							    "<td style='width:15%'><span propsId='"+data[i].id+"' class='propsButton pokeyButton pokeyButton-1 button-blue-1 icon' ><span class='text-gm num_icon'></span></span></td>"+
							  "</tr>");
				}
				$("#propsLists").html(lists.join(""));
				$(".propsButton").click(function(){
					var propsId = $(this).attr("propsId");
					_t.buyProps(propsId);
				})
			},
			
			//item/buy  (POST):参数 userId, itemToken
			buyProps:function(propsId){
				var userId = window.sessionStorage.getItem(MyOwner.userId);
				var url = Game.getBaseURL() + "/item/buy";
				$.ajax({
					url: url,
					type: "POST",
					data: "userId="+userId+"&itemId="+propsId,
					dataType:"json",
					success: function(data){
						if(data.success){
							$().alert({
								content:data.message
							});
						}else{
							$().alert({
								content:data.message
							});
						}
					}
				});
			},
			
			createItemList:function(data){
				
				if(data.length === 0){
					$().alert({
						content:"无道具可用"
					})
				}else{
					var lists = [];
					for(var i=0; i<data.length; i++){
						if(i === 0){
							lists.push("<li class='item itemOn props_icon' name='"+data[i].itemName+"' description='"+data[i].itemDescription+"' token='"+data[i].token+"'><span class='props_icon props_"+data[i].token+"'></span></li>")
						}else{
							lists.push("<li class='item props_icon' name='"+data[i].itemName+"' description='"+data[i].itemDescription+"' token='"+data[i].token+"'><span class='props_icon props_"+data[i].token+"'></span></li>")
						}
						
					}
					
					Box.propsList(lists.join(""),data[0]);
					//$("#propsLists").html(lists.join(""));
				}
				
				
			},
			
			/*使用道具*/
			use:function(token){
				var _t = this;
				var url = Game.getBaseURL() + "/item/";
				$.ajax({
					url: url,
					dataType:"json",
					success: function(data){
						if(data.success){
							_t.createItemList(data.data.userItems);
						}
					}
				});
			}
	}
    
	
	
	return {
		
		myList:function(){console.log(111);
			props.myList();
			console.log(2);
		},
		
		allList:function(){
			props.allList();
		},
		
		roomList:function(){
			props.listRoom();
		}
		
		
		
		
		
	}
}); 